import MySQLdb
import urllib2
import config
import datetime
import time
from datetime import datetime
from datetime import date
from time import mktime
import hashlib



def hash_url(url):
    return hash(url)

def get_content(url):
    try:
        response = urllib2.urlopen(url)
        content = response.read()
        return content
    except:
        return ''
    

def create_conn():
    conn = MySQLdb.connect (host=config.mysql_local,
                            user=config.mysql_user,
                            passwd=config.mysql_pass,
                            db=config.mysql_db,
                            port=config.mysql_port,
                            charset='utf8',
                            use_unicode=False,
                            )
    conn.autocommit(True)
    return conn


def get_unixtime(intime):
    print datetime
    mytime = datetime.strptime(intime,'%Y-%m-%d')
    return int(mktime(mytime.timetuple()))

def get_timeunix_today():
    mydate = date.today()
    return int(mktime(mydate.timetuple()))
def get_date(time_unix):
    return (datetime.fromtimestamp(int(time_unix)).strftime('%Y-%m-%d'))
def get_datetime(time_unix):
    return (datetime.fromtimestamp(int(time_unix)).strftime('%Y-%m-%d %H:%m'))

def get_date_hour():
    mytime = time.strftime('%Y-%m-%d %H:00')
    return mytime
def get_result_sql(conn,cmd):
    try:
        conn.ping()
    except:
        conn = create_conn()
    cursor = conn.cursor(MySQLdb.cursors.DictCursor)
    cursor.execute(cmd)
    result = cursor.fetchall()
    cursor.close()
    return result

def get_current_date(time_date):
    return(datetime.datetime.fromtimestamp(int(time_date)).strftime('%d-%m-%Y'))
    (datetime.datetime.fromtimestamp(int(time.time())))

if __name__ == "__main__":
    
    print datetime.now()